/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package byzantine.controls.listeners;

import byzantine.ByzantineModel;
import byzantine.controls.SelectedGeneralControls;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;

/**
 *
 * @author Ryhan
 */
public class SelectGeneralListener implements ItemListener {

    SelectedGeneralControls generalControls;
    ByzantineModel model;

    public SelectGeneralListener(SelectedGeneralControls generalControls, ByzantineModel model) {
        this.generalControls = generalControls;
        this.model = model;
    }

    public void itemStateChanged(ItemEvent event) {

        JComboBox cb = (JComboBox) event.getSource();
        if (cb.getSelectedItem() == null) {
            return;
        }

        String selectedItem = cb.getSelectedItem().toString();

        if (event.getStateChange() == ItemEvent.SELECTED) {
            String tokens[] = selectedItem.split(" ");
            if (tokens.length > 1) {
                Integer ID = Integer.parseInt(tokens[0]);
                model.setSelectedGeneral(model.getGeneralByID(ID));
            } else {
                model.setSelectedGeneral(null);
            }
        } else if (event.getStateChange() == ItemEvent.DESELECTED) {
            // Item is no longer selected
        }
    }
}
